package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.Area;
import com.ruoyi.system.domain.qo.UserListQo;
import com.ruoyi.system.domain.vo.AppUserVO;
import com.ruoyi.system.domain.vo.IndexVo;
import com.ruoyi.system.domain.vo.UserAdminListVO;
import com.ruoyi.system.mapper.SysUserDeviceBindMapper;
import com.ruoyi.system.service.IAreaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AppUserMapper;
import com.ruoyi.system.service.IAppUserService;

/**
 * 用户管理 服务层实现
 *
 * @author ruoyi
 */
@Service
public class AppUserServiceImpl implements IAppUserService {
    private final AppUserMapper appUserMapper;

    private final IAreaService areaService;

    private final SysUserDeviceBindMapper sysUserDeviceBindMapper;

    private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    public AppUserServiceImpl(AppUserMapper appUserMapper, IAreaService areaService, SysUserDeviceBindMapper sysUserDeviceBindMapper) {
        this.appUserMapper = appUserMapper;
        this.areaService = areaService;
        this.sysUserDeviceBindMapper = sysUserDeviceBindMapper;
    }

    /**
     * 查询用户信息
     *
     * @param id 用户ID
     * @return 用户信息
     */
    @Override
    public AppUser selectAppUserById(Long id) {
        AppUser appUser = appUserMapper.selectAppUserById(id);
        if (Objects.nonNull(appUser)) {
            Area area = areaService.selectAreaById(appUser.getAreaId());
            if (Objects.nonNull(area)) {
                appUser.setAreaName(area.getAreaName());
            }
        }
        return appUser;
    }

    /**
     * 查询用户列表
     *
     * @param appUser 用户信息
     * @return 用户集合
     */
    @Override
    public List<AppUser> selectAppUserList(AppUser appUser) {
        return appUserMapper.selectAppUserList(appUser);
    }

    /**
     * 查询应用用户列表
     *
     * @return 应用用户列表
     */
    @Override
    public List<AppUserVO> selectUserList(UserListQo userListQo) {
        return appUserMapper.selectUserList(userListQo);
    }

    @Override
    public List<UserAdminListVO> selectUserAdminList(UserListQo userListQo) {
        return appUserMapper.selectUserAdminList(userListQo);
    }

    /**
     * 新增用户
     *
     * @param appUser 用户信息
     * @return 结果
     */
    @Override
    public int insertAppUser(AppUser appUser) {
        if (StringUtils.isEmpty(appUser.getLoginName())) {
            throw new ServiceException("用户名不能为空");
        }
        if (!checkUserNameUnique(appUser)) {
            throw new ServiceException("保存用户'" + appUser.getLoginName() + "'失败，注册账号已存在");
        }

        if (StringUtils.isEmpty(appUser.getLoginPassword())) {
            String defaultPassword = "123456";
            appUser.setLoginPassword(passwordEncoder.encode(defaultPassword));
        } else {
            appUser.setLoginPassword(passwordEncoder.encode(appUser.getLoginPassword()));
        }
        return appUserMapper.insertAppUser(appUser);
    }

    /**
     * 修改用户
     *
     * @param appUser 用户信息
     * @return 结果
     */
    @Override
    public int updateAppUser(AppUser appUser) {
        return appUserMapper.updateAppUser(appUser);
    }

    /**
     * 删除用户信息
     *
     * @param id 用户ID
     * @return 结果
     */
    @Override
    public int deleteAppUserById(Long id) {
        return appUserMapper.deleteAppUserById(id);
    }

    /**
     * 批量删除用户信息
     *
     * @param ids 需要删除的用户ID
     * @return 结果
     */
    @Override
    public int deleteAppUserByIds(Long[] ids) {
        return appUserMapper.deleteAppUserByIds(ids);
    }

    @Override
    public Map<String, Long> getServiceUserCount() {
        Map<String, Long> serviceUserCount = new HashMap<>();
        Long bindUser = appUserMapper.getServiceUserCount(null);
        Long wearUser = appUserMapper.getServiceUserCount("1");
        serviceUserCount.put("bindUser", bindUser);
        serviceUserCount.put("wearUser", wearUser);
        return serviceUserCount;
    }

    @Override
    public Map<String, List<IndexVo>> countUserByHour() {
        Map<String, List<IndexVo>> result = new HashMap<>();
        List<IndexVo> userHour = appUserMapper.selectUserRegisterHourlyStats();
        result.put("用户注册", userHour);
        List<IndexVo> bindHour = sysUserDeviceBindMapper.selectUserBindHourlyStats();
        result.put("手环绑定", bindHour);
        return result;
    }

    @Override
    public Map<String, List<IndexVo>> countUserByDay() {
        Map<String, List<IndexVo>> result = new HashMap<>();
        List<IndexVo> userDaily = appUserMapper.selectUserRegisterDailyStats();
        result.put("用户注册", userDaily);
        List<IndexVo> bindDaily = sysUserDeviceBindMapper.selectUserBindDailyStats();
        result.put("手环绑定", bindDaily);
        return result;
    }

    @Override
    public int updatePwd(Long id, String password) {
        return appUserMapper.updatePwd(id, password);
    }

    /**
     * 校验用户名称是否唯一
     *
     * @param user 用户信息
     * @return 结果
     */
    public boolean checkUserNameUnique(AppUser user) {
        AppUser info = appUserMapper.checkUserName(user.getLoginName());
        if (Objects.nonNull(info) && !Objects.equals(info.getId(), user.getId())) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}